package org.jivesoftware.smack;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.WeakHashMap;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Privacy;
import org.jivesoftware.smack.packet.PrivacyItem;

public class PrivacyListManager
{
  private static Map<Connection, PrivacyListManager> instances = Collections.synchronizedMap(new WeakHashMap());
  private WeakReference<Connection> connection;
  private final List<PrivacyListListener> listeners = new ArrayList();
  PacketFilter packetFilter = new AndFilter(new PacketFilter[] { new IQTypeFilter(IQ.Type.SET), new PacketExtensionFilter("query", "jabber:iq:privacy") });

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated(Connection paramAnonymousConnection)
      {
        PrivacyListManager.getInstanceFor(paramAnonymousConnection);
      }
    });
  }

  private PrivacyListManager(final Connection paramConnection)
  {
    this.connection = new WeakReference(paramConnection);
    instances.put(paramConnection, this);
    paramConnection.addPacketListener(new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        if ((paramAnonymousPacket == null) || (paramAnonymousPacket.getError() != null))
          return;
        Privacy localPrivacy = (Privacy)paramAnonymousPacket;
        synchronized (PrivacyListManager.this.listeners)
        {
          Iterator localIterator1 = PrivacyListManager.this.listeners.iterator();
          while (localIterator1.hasNext())
          {
            PrivacyListListener localPrivacyListListener = (PrivacyListListener)localIterator1.next();
            Iterator localIterator2 = localPrivacy.getItemLists().entrySet().iterator();
            while (localIterator2.hasNext())
            {
              Map.Entry localEntry = (Map.Entry)localIterator2.next();
              String str = (String)localEntry.getKey();
              List localList = (List)localEntry.getValue();
              if (localList.isEmpty())
                localPrivacyListListener.updatedPrivacyList(str);
              else
                localPrivacyListListener.setPrivacyList(str, localList);
            }
          }
        }
        ??? = new IQ()
        {
          public String getChildElementXML()
          {
            return "";
          }
        };
        ((IQ)???).setType(IQ.Type.RESULT);
        ((IQ)???).setFrom(paramAnonymousPacket.getFrom());
        ((IQ)???).setPacketID(paramAnonymousPacket.getPacketID());
        paramConnection.sendPacket((Packet)???);
      }
    }
    , this.packetFilter);
  }

  private String getUser()
  {
    return ((Connection)this.connection.get()).getUser();
  }

  public static synchronized PrivacyListManager getInstanceFor(Connection paramConnection)
  {
    PrivacyListManager localPrivacyListManager = (PrivacyListManager)instances.get(paramConnection);
    if (localPrivacyListManager == null)
      localPrivacyListManager = new PrivacyListManager(paramConnection);
    return localPrivacyListManager;
  }

  private Privacy getRequest(Privacy paramPrivacy)
    throws XMPPException
  {
    Connection localConnection = (Connection)this.connection.get();
    if (localConnection == null)
      throw new XMPPException("Connection instance already gc'ed");
    paramPrivacy.setType(IQ.Type.GET);
    paramPrivacy.setFrom(getUser());
    PacketIDFilter localPacketIDFilter = new PacketIDFilter(paramPrivacy.getPacketID());
    PacketCollector localPacketCollector = localConnection.createPacketCollector(localPacketIDFilter);
    localConnection.sendPacket(paramPrivacy);
    Privacy localPrivacy = (Privacy)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localPrivacy == null)
      throw new XMPPException("No response from server.");
    if (localPrivacy.getError() != null)
      throw new XMPPException(localPrivacy.getError());
    return localPrivacy;
  }

  private Packet setRequest(Privacy paramPrivacy)
    throws XMPPException
  {
    Connection localConnection = (Connection)this.connection.get();
    if (localConnection == null)
      throw new XMPPException("Connection instance already gc'ed");
    paramPrivacy.setType(IQ.Type.SET);
    paramPrivacy.setFrom(getUser());
    PacketIDFilter localPacketIDFilter = new PacketIDFilter(paramPrivacy.getPacketID());
    PacketCollector localPacketCollector = localConnection.createPacketCollector(localPacketIDFilter);
    localConnection.sendPacket(paramPrivacy);
    Packet localPacket = localPacketCollector.nextResult(20000L);
    localPacketCollector.cancel();
    if (localPacket == null)
      throw new XMPPException("No response from server.");
    if (localPacket.getError() != null)
      throw new XMPPException(localPacket.getError());
    return localPacket;
  }

  private Privacy getPrivacyWithListNames()
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    return getRequest(localPrivacy);
  }

  public PrivacyList getActiveList()
    throws XMPPException
  {
    Privacy localPrivacy = getPrivacyWithListNames();
    String str = localPrivacy.getActiveName();
    boolean bool = (localPrivacy.getActiveName() != null) && (localPrivacy.getDefaultName() != null) && (localPrivacy.getActiveName().equals(localPrivacy.getDefaultName()));
    return new PrivacyList(true, bool, str, getPrivacyListItems(str));
  }

  public PrivacyList getDefaultList()
    throws XMPPException
  {
    Privacy localPrivacy = getPrivacyWithListNames();
    String str = localPrivacy.getDefaultName();
    boolean bool = (localPrivacy.getActiveName() != null) && (localPrivacy.getDefaultName() != null) && (localPrivacy.getActiveName().equals(localPrivacy.getDefaultName()));
    return new PrivacyList(bool, true, str, getPrivacyListItems(str));
  }

  private List<PrivacyItem> getPrivacyListItems(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy1 = new Privacy();
    localPrivacy1.setPrivacyList(paramString, new ArrayList());
    Privacy localPrivacy2 = getRequest(localPrivacy1);
    return localPrivacy2.getPrivacyList(paramString);
  }

  public PrivacyList getPrivacyList(String paramString)
    throws XMPPException
  {
    return new PrivacyList(false, false, paramString, getPrivacyListItems(paramString));
  }

  public PrivacyList[] getPrivacyLists()
    throws XMPPException
  {
    Privacy localPrivacy = getPrivacyWithListNames();
    Set localSet = localPrivacy.getPrivacyListNames();
    PrivacyList[] arrayOfPrivacyList = new PrivacyList[localSet.size()];
    int i = 0;
    Iterator localIterator = localSet.iterator();
    while (localIterator.hasNext())
    {
      String str = (String)localIterator.next();
      boolean bool1 = str.equals(localPrivacy.getActiveName());
      boolean bool2 = str.equals(localPrivacy.getDefaultName());
      arrayOfPrivacyList[i] = new PrivacyList(bool1, bool2, str, getPrivacyListItems(str));
      i++;
    }
    return arrayOfPrivacyList;
  }

  public void setActiveListName(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setActiveName(paramString);
    setRequest(localPrivacy);
  }

  public void declineActiveList()
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setDeclineActiveList(true);
    setRequest(localPrivacy);
  }

  public void setDefaultListName(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setDefaultName(paramString);
    setRequest(localPrivacy);
  }

  public void declineDefaultList()
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setDeclineDefaultList(true);
    setRequest(localPrivacy);
  }

  public void createPrivacyList(String paramString, List<PrivacyItem> paramList)
    throws XMPPException
  {
    updatePrivacyList(paramString, paramList);
  }

  public void updatePrivacyList(String paramString, List<PrivacyItem> paramList)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setPrivacyList(paramString, paramList);
    setRequest(localPrivacy);
  }

  public void deletePrivacyList(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setPrivacyList(paramString, new ArrayList());
    setRequest(localPrivacy);
  }

  public void addListener(PrivacyListListener paramPrivacyListListener)
  {
    synchronized (this.listeners)
    {
      this.listeners.add(paramPrivacyListListener);
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.PrivacyListManager
 * JD-Core Version:    0.6.1
 */